import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "../components/layout/layout.vue";
import index from "../views/index/index.vue";
import login from "../views/login/login.vue";
import user from "../views/user/user.vue";
import role from "../views/role/role.vue";
import page from "../views/page/page.vue";
import component from "../views/component/component.vue";
import componentPermission from "../views/component/componentPermission/componentPermission.vue";
import permission from "../views/role/permission/permission.vue";
import {lookRouterTable} from "@/api/api"

Vue.use(VueRouter);

//默认路由
const routes = [
  {
    path: "",
    name: "login",
    component: login,
    children: [
      {
        path: "/",
        component: login
      },
      {
        path: "/login",
        component: login
      }
    ]
  },
  {
    path: "/index",
    name: "index",
    component: Layout,
    meta: {
      icon: "index",
      isLogin: false
    },
    children: [
      {
        path: "",
        name: "index",
        component: index,
        meta: {
          icon: "index",
          isLogin: false
        }
      }
    ]
  },
  {
    path: "/user",
    name: "用户管理",
    component: Layout,
    meta: {
      icon: "user",
      isLogin: false
    },
    children: [
      {
        path: "",
        name: "用户管理",
        component: user,
        meta: {
          icon: "user",
          isLogin: false
        }
      }
    ]
  },
  {
    path: "/role",
    name: "角色管理",
    component: Layout,
    meta: {
      icon: "role",
      isLogin: false
    },
    children: [
      {
        path: "",
        name: "角色管理",
        component: role,
        meta: {
          icon: "role",
          isLogin: false
        }
      },
      {
        path: "/permission",
        name: "角色管理",
        component: permission,
        props: (route) => ({ roleId: route.query.roleId,roleName: route.query.roleName}),
        meta: {
          icon: "role",
          isLogin: false
        }
      }
    ]
  },
  {
    path: "/page",
    name: "页面管理",
    component: Layout,
    meta: {
      icon: "page",
      isLogin: false
    },
    children: [
      {
        path: "",
        name: "页面管理",
        component: page,
        meta: {
          icon: "page",
          isLogin: false
        }
      }
    ]
  },
  {
    path: "/component",
    name: "组件管理",
    component: Layout,
    meta: {
      icon: "conponent",
      isLogin: false
    },
    children: [
      {
        path: "",
        name: "组件管理",
        component: component,
        meta: {
          icon: "page",
          isLogin: false
        }
      },
      {
        path: "componentPermission",
        name: "组件权限设置",
        component: componentPermission,
        props: (route) => ({ componentId: route.query.componentId,componentName: route.query.componentName}),
        meta: {
          icon: "page",
          isLogin: false
        }
      }
    ]
  }
];

const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes
});

//设置全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.isLogin)) {
    //判断是否需要登录
    if (sessionStorage["username"]) {
      next();
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath
        }
      });
    }
  } else {
    next();
  }
});
const activeRouter=async function(){
   await lookRouterTable({roleId:'ad9bf1c7-a890-4229-9b6d-666d51205cf7'}).then(data=>{
       getRouterTable(data.data)
  })
}
function getRouterTable(tData){
  return tData.reduce(function sortTree(prev,current,index,array){
   let cArray=[...array]
   cArray.splice(index,1)
   if(current.nodeType=="根节点"){
       let returnData=cArray.length==0?[]:cArray.reduce(sortTree,[current.nodeId])
       let routerNode={
         role:current.roleName,
         type:"根节点"
       }
       returnData==undefined||returnData.length==0?"":(routerNode.children=returnData)
       prev.push(routerNode)
   }else if(current.nodeType=="目录"&&prev[0]==current.parentId){
       let returnData=cArray.length==0?[]:cArray.reduce(sortTree,[current.nodeId])
       let routerNode={
        path:current.page.pagePath,
        name:current.page.pageName,
        component:current.component==null?"":current.component.componentValue,
        redirect:current.page.redirect,
        type:'目录',
        meta:{
          isLogin:current.page.isLogin,
          icon:current.page.pageIcon,
          bgColor:current.page.bgColor,
          permission:{},
        },
        props: (route) => ({...route.query}),
      }
      if(returnData.length!=0&&typeof(returnData)!=undefined){
        for(let item in returnData){
          if(returnData[item].type=="组件"&&returnData[item].children.length!=0){
             for(index in returnData[item].children){
              routerNode.meta.permission={...routerNode.meta.permission,...returnData[item].children[index]}
             }
          }else if(returnData[item].type=="目录"){
            routerNode.children=routerNode.children?[...routerNode.children,returnData[item]]:[returnData[item]]
          }
        }
      }
       prev.push(routerNode)
   }else if(current.nodeType=="组件"&&current.parentId==prev[0]){
       let returnData=cArray.length==0?[]:cArray.reduce(sortTree,[current.nodeId])
       let routerNode={
        type:"组件",
        children:[]
      }
       if(returnData.length!=0){
        for(let  item in returnData){
          if(returnData[item].type=="权限属性节点"){
            routerNode.children=[...routerNode.children,returnData[item].value]
          }
        }
      }
       prev.push(routerNode)
   }else if(current.nodeType=="权限属性节点"&&current.parentId==prev[0]){
       let routerNode={
        type:"权限属性节点",
        value:{[current.componentPermission.componentPermissionName]:current.componentPermission.componentPermissionValue}
      }
       prev.push(routerNode)
   }
   if(index>=array.length-1){
     prev.splice(0,1)
   }
   return prev
},[''])};

activeRouter()
// router.addRoutes(activeRouter)

export default router;
